package filters;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import session.UserContainer;
import web.IConstants;

/**
 * @web.filter name = "AuthenticationFilter" description = "Validates that a
 *             user has a valid session before allowing them to proceed to an
 *             .thm action"
 * @web.filter-mapping url-pattern="/auth/*"
 * @author ipeel
 */
public class AuthenticationFilter implements Filter {
    /*
     * (non-Javadoc)
     * 
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    public void init(FilterConfig config) throws ServletException {

    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
     *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        HttpSession session = httpRequest.getSession();
        
        String context = null;
        context = httpRequest.getContextPath();

        UserContainer uc = (UserContainer)session.getAttribute(IConstants.Sessoin.USER_CONTAINER);
        
        if(uc == null) {
        	uc = new UserContainer();
        }
        
        if (session == null || uc.getUser() == null) {
            if (session == null) {
                session = httpRequest.getSession(true);
            }
            
            Enumeration names = request.getParameterNames();
            List<String> paramaters = new ArrayList<String>();
            
            while(names.hasMoreElements()) {
            	String param = (String)names.nextElement();
            	paramaters.add(new String(param+"="+request.getParameter(param)));
            }
            //Do this to remember where the user is tryting to go so we can send them back after authentication.
            uc.setPreAuthParams(paramaters);
            uc.setPreAuthURI(httpRequest.getRequestURI());
            
            session.setAttribute(IConstants.Sessoin.USER_CONTAINER, uc);
            
            httpResponse.sendRedirect(context+IConstants.Views.LOGIN_REDIRECT);
            return;
        }

        chain.doFilter(request, response);
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.servlet.Filter#destroy()
     */
    public void destroy() {

    }
}
